$(domReady);

function domReady() {
	
    // Göm alla templates
    $(SNAIL.Config.templateSelector).hide();
	
    // Gör hela "logon" klickbar
    $('#snail div.title').clickMe();
	
    // Kräv bekräftelse för...
    $('.confirm').confirm({ 
        confirmText: SNAIL.Config.messages.confirm 
    });
    $('.confirmLogout').confirm({ 
        confirmText: SNAIL.Config.messages.confirmLogout 
    });
    $('.confirmUser').confirm({ 
        confirmText: SNAIL.Config.messages.confirmRemoveUser 
    });

    // Initiera auktioner
    SNAIL.Auctions.init();
	
}
SNAIL.User =
{
    rate:function(articleId, positive){
        $.ajax({
            type: "POST",
            url: "/Snail_Titanium/user/rate",
            data: "id=" + articleId + "&pos=" + positive,
            success: function(msg){
                if(msg.indexOf('error.') == 0)
                    SNAIL.Messages.showError(msg.substring(msg.indexOf(".") + 1, msg.length - 1));
                else
                {
                    $("#rate-0").remove();
                    $("#rate-1").replaceWith("Tack!");
                    SNAIL.Messages.showInfo(msg);
                }
            }
        });
    }
};
SNAIL.Auctions = {

    currency: ' kr',
	
    update: function(id, bid) {
        var a = $('#a-' + id);
        var bidDom = a.find('.bid');
        bidDom.addClass('changing', 200, function() {
            bidDom.html(bid + SNAIL.Auctions.currency);
            bidDom.removeClass('changing', 800);
        });
    },
	
    init: function() {
        var auction = $(SNAIL.Config.auction.selector);
        var controlsTemplate =
        $(SNAIL.Config.auction.controls.templateSelector)
        .removeClass(SNAIL.Config.templateClass);
		
        // Visa kontroller f�r auktioner p� mouseOver
        auction.hover(function() {
            var a = $(this);
            var auctionId = a.attr('id').replace('a-', '');
            var div = controlsTemplate.clone();
            a.append(div);

            if(!a.hasClass('created'))
                div.find(SNAIL.Config.auction.controls.deleteSelector).parent().hide();
            
            var watchBtn = div.find(SNAIL.Config.auction.controls.watchSelector);
            var watch = true;
            if(a.hasClass('watching')) {
                watchBtn.html('Avsluta bevakning');
                watch = false;
            } else {
                watchBtn.html('Bevaka');
            }
			
            // Positionera verktygsf�lt
            var leftPos = a.width() + a.position().left - div.width() - 8 + 'px';
            var topPos = a.height() + a.position().top - div.height() - 10 + 'px';
            div.css({
                'left' : leftPos,
                'top' : topPos
            });
			
            // Event binding
            watchBtn
            .click(function() {
                div.fadeOut();
                if(watch) SNAIL.Auctions.onWatch(auctionId);
                else SNAIL.Auctions.unWatch(auctionId);
            });
            div.find(SNAIL.Config.auction.controls.deleteSelector)
            .click(function() {
                if(confirm(SNAIL.Config.messages.confirmRemoveAuction)) {
                    div.fadeOut();
                    SNAIL.Auctions.onDelete(auctionId);
                }
            });
            div.find(SNAIL.Config.auction.controls.tipSelector)
            .click(function() {
                div.fadeOut();
                SNAIL.Auctions.onTip(auctionId);
            });
			
            // Visa verktygsfält
            div.fadeIn(SNAIL.Config.animation.fast);
        },
        function() {
            $(this).find('.controls').remove();
        })
		
        // gör hela auktionen klickbar
        .clickMe({
            ignoreClass: SNAIL.Config.ignoreClass
        });
		
    },

    unWatch: function(articleId){
        $.ajax({
            type: "POST",
            url: "/Snail_Titanium/articles/unwatch",
            data: "id=" + articleId,
            success: function(msg){
                if(msg.indexOf('error.') == 0)
                    SNAIL.Messages.showError(msg.substring(msg.indexOf(".") + 1, msg.length - 1));
                else
                {
                    $('#a-' + articleId).removeClass('watching');
                    $("#rrr-"+ articleId).slideUp();
                    $("#www-"+ articleId).slideUp();
                    SNAIL.Messages.showInfo(msg);
                }
            }
        });
    },
	
    onWatch: function(auctionId) {
        $.ajax({
            type: "POST",
            url: "watch",
            data: "id=" + auctionId,
            success: function(msg){
                if(msg.indexOf('error.') == 0) {
                    SNAIL.Messages.showError(msg.substring(msg.indexOf(".") + 1, msg.length - 1));
                } else {
                    $('#a-' + auctionId).addClass('watching');
                    $("#sidebar>ul").prepend("<li id='www-"+ auctionId +"'><a href=\"/Snail_Titanium/articles/details/"+auctionId+"\">" + $("#a-" + auctionId + ">.info>h4>a").html() + "</a></li>").hide().fadeIn();
                    SNAIL.Messages.showInfo(msg);
                }
            }
        });

    //SNAIL.Messages.showError('Bevaka auktion ' + auctionId + ' - Ej implementerat');
    },
	
    onDelete: function(auctionId) {
        $.ajax({
            type: "POST",
            url: "/Snail_Titanium/articles/remove",
            data: "id=" + auctionId,
            success: function(msg){
                if(msg.indexOf('error.') == 0)
                    SNAIL.Messages.showError(msg.substring(msg.indexOf(".") + 1, msg.length - 1));
                else
                {
                    $('#a-' + auctionId).slideUp();
                    SNAIL.Messages.showInfo(msg);
                }
            }
        });
        
    },
	
    onTip: function(auctionId) {
        SNAIL.Messages.showError('Tipsa en vän om auktion ' + auctionId + ' - Ej implementerat');
    },

    setWatched: function(auctionId) {
        alert(auctionId);
    }
	
};

SNAIL.Messages = {
    nErr : 0,
    errTimer : null,
    
    showError: function(message) {
        this.show(message, 1);
    },
    
    showInfo : function(message) {
        this.show(message, 2);
    },

    show : function(error, type) {
        errorPane = $("#messages");
        if(errorPane.html() == null) {
            errorPane =
            $('<div id="messages"></div>')
            .appendTo('body');
        }
        
        clearTimeout(this.errTimer);
        this.errTimer = setTimeout('$("#messages").fadeOut(300, function() {$(this).remove()});', 5000);

        var errDiv;
        switch(type) {
            case 1:
                errDiv = $(this.getErrorDiv(error));
                break;
            case 2:
                errDiv = $(this.getInfoDiv(error));
                break;
        }
        
        errDiv.appendTo(errorPane);
        
        setTimeout('$(".nErr_'+ (this.nErr - 1) +'").slideUp(700)', 4300);
        
        errorPane
        .slideDown('slow');
    },
    
    getErrorDiv : function(errorMessage) {
        return '<div class="error nErr_' + (this.nErr++) + '">' + errorMessage + '</div>';
    },
    
    getInfoDiv : function(message) {
        return '<div class="info nErr_' + (this.nErr++) + '">' + message + '</div>';    
    }
    
};
